﻿#include "form.h"
#include "ui_form.h"
#include <QDebug>
#include <QString>

Form::Form(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Form)
{
    ui->setupUi(this);

    initUi();
}

Form::~Form()
{
qDebug() << "~Form()";
    delete ui;
}

//初始化UI
void Form::initUi()
{
    //1. 发现设备
    findSystemObj();

    //2. 显示采集方式选项到列表: 单图像采集 + 连续采集
    QStringList EAcquisitionTypeList;
    EAcquisitionTypeList << "Continuous" << "SingleFrame";
    ui->comboBoxEAcquisitionType->addItems(EAcquisitionTypeList);

    //3. 显示帧率选项
    QStringList FPSList;
    FPSList << "30FPS" << "40FPS" << "50FPS" << "60FPS"
            << "80FPS" << "100FPS" << "120FPS" << "150FPS"
            << "200FPS" << "250FPS" << "300FPS" << "400FPS";
    ui->comboBoxFPS->addItems(FPSList);
    ui->comboBoxFPS->setCurrentIndex(5);

    //4. 设置保存图片张数选项
}

//发现设备
void Form::findSystemObj()
{
    CSystem  &systemObj = CSystem::getInstance();
    //发现设备
    bool isDiscoverySuccess = systemObj.discovery(m_vCameraPtrList);

    if (!isDiscoverySuccess)
    {
        ui->comboBoxCameraKey->setEnabled(false);

        widgetControl(findSystemObjFail);
    }
    else
    {
        widgetControl(findSystemObjSuccess);
    }

    //显示当前相机列表到界面
    for(int i = 0; i < m_vCameraPtrList.size(); i++)
    {
        ui->comboBoxCameraKey->addItem(m_vCameraPtrList[i]->getKey());
    }
}

//设置选择的相机
void Form::on_comboBoxCameraKey_currentIndexChanged(int nIndex)
{
    ui->ShowWidget->SetCamera(m_vCameraPtrList[nIndex]->getKey());
}

//设置帧率
void Form::on_comboBoxFPS_currentIndexChanged()
{
    //获取选项文字
    QString FPSText = ui->comboBoxFPS->currentText();
    //取出其中FPS数字
    QString tmp;
    for(int j = 0; j < FPSText.length(); j++)
    {
        if(FPSText[j] >= '0' && FPSText[j] < '9')
        {
            tmp.append(FPSText[j]);
        }
    }

    //设置改变后的FPS值
    ui->ShowWidget->FPSSet(tmp.toInt());
}

//设置选择的采集方式
void Form::comboBoxEAcquisitionModeSet(int nIndex)
{
    if(0 == nIndex)
    {
        ui->comboBoxFPS->setEnabled(true);
        ui->ShowWidget->CameraChangeAcquisitionMode(AcquisitionMode::Continuous);
    }
    else if(1 == nIndex)
    {
        ui->comboBoxFPS->setEnabled(false);
        ui->ShowWidget->CameraChangeAcquisitionMode(AcquisitionMode::SingleFrame);
    }
}

//连接刷新按钮槽函数
void Form::on_pushButtonRefresh_clicked()
{
    //清空当前选项表
    ui->comboBoxCameraKey->blockSignals(true);
    ui->comboBoxCameraKey->clear();
    ui->comboBoxCameraKey->blockSignals(false);
    //重新发现设备
    findSystemObj();
}

//连接断开按钮槽函数
void Form::on_pushButtonConnectDisconnect_clicked()
{
    //获取按钮当前文字
    QString pBText = ui->pushButtonConnectDisconnect->text();

    //根据文字进行操作
    if("Connect" == pBText)
    {
        bool bet = ui->ShowWidget->CameraOpen();
        if(bet)
        {
            widgetControl(ConnectStatus);
        }
    }
    else if("Disconnect" == pBText)
    {
qDebug() << "Disconnect*****************";
        //停止相机
        ui->ShowWidget->CameraStop();
qDebug() << "bbb";
        //关闭相机
        bool bet = ui->ShowWidget->CameraClose();
qDebug() << "bet: " << bet;
        if(bet)
        {
            widgetControl(DisConnectStatus);
        }
    }
 qDebug() << "ccc" ;
}

//启动关闭相机槽函数
void Form::on_pushButtonStartStop_clicked()
{
    //获取按钮当前文字
    QString pBText = ui->pushButtonStartStop->text();
    if("Start" == pBText)
    {
        //设置采集模式
        int nIndex = ui->comboBoxEAcquisitionType->currentIndex();
        comboBoxEAcquisitionModeSet(nIndex);
        //启动相机
        bool bet = ui->ShowWidget->CameraStart();
        if(bet)
        {
            widgetControl(StartStatus);
        }
    }
    else if("Stop" == pBText)
    {
        //停止相机
        bool bet = ui->ShowWidget->CameraStop();
        if(bet)
        {
            widgetControl(StopStatus);
        }
    }
}

//界面控件状态控制函数
void Form::widgetControl(ESystemStatusType SystemStatusType)
{
    switch (SystemStatusType)
    {
    case findSystemObjSuccess:
    {
        //设置显示文字
        ui->pushButtonConnectDisconnect->setText("Connect");
        ui->pushButtonStartStop->setText("Start");
        //设置权限
        ui->comboBoxCameraKey->setEnabled(true);
        ui->pushButtonConnectDisconnect->setEnabled(true);
        ui->pushButtonStartStop->setEnabled(false);
        ui->pushButtonRefresh->setEnabled(true);
        ui->comboBoxEAcquisitionType->setEnabled(false);
        break;
    }
    case findSystemObjFail:
    {
        //设置显示文字
        ui->pushButtonConnectDisconnect->setText("Connect");
        ui->pushButtonStartStop->setText("Start");
        //设置权限
        ui->comboBoxCameraKey->setEnabled(true);
        ui->pushButtonConnectDisconnect->setEnabled(false);
        ui->pushButtonStartStop->setEnabled(false);
        ui->pushButtonRefresh->setEnabled(true);
        ui->comboBoxEAcquisitionType->setEnabled(false);
        break;
    }
    case ConnectStatus:
    {
        //设置显示文字
        ui->pushButtonConnectDisconnect->setText("Disconnect");
        ui->pushButtonStartStop->setText("Start");
        //设置权限
        ui->comboBoxCameraKey->setEnabled(false);
        ui->pushButtonConnectDisconnect->setEnabled(true);
        ui->pushButtonStartStop->setEnabled(true);
        ui->pushButtonRefresh->setEnabled(false);
        ui->comboBoxEAcquisitionType->setEnabled(true);
        break;
    }
    case DisConnectStatus:
    {
        //设置显示文字
        ui->pushButtonConnectDisconnect->setText("Connect");
        ui->pushButtonStartStop->setText("Start");
        //设置权限
        ui->comboBoxCameraKey->setEnabled(true);
        ui->pushButtonConnectDisconnect->setEnabled(true);
        ui->pushButtonStartStop->setEnabled(false);
        ui->pushButtonRefresh->setEnabled(true);
        ui->comboBoxEAcquisitionType->setEnabled(false);
        break;
    }
    case StartStatus:
    {
        //设置显示文字
        ui->pushButtonStartStop->setText("Stop");
        //设置权限
        ui->comboBoxEAcquisitionType->setEnabled(false);
        break;
    }
    case StopStatus:
    {
        //设置显示文字
        ui->pushButtonStartStop->setText("Start");
        //设置权限
        ui->comboBoxEAcquisitionType->setEnabled(true);
        break;
    }
    }
}

//关闭界面函数
void Form::closeEvent(QCloseEvent * event)
{
    ui->ShowWidget->CameraClose();
    widgetControl(DisConnectStatus);
}





























